<template>
	<view class="container">
		<view class="yy-cel-reson">
			<text>请选择取消原因</text>
			<view class="yy-cel-resonNav">
				<view :class="['yy-cel-resonItem',item.check == true?'yy-cel-resonactive':'']" v-for="(item,index) in reasonlist" :key="index" @click="choosereason(item.name,index)">{{item.name}}</view>
			</view>
		</view>
		<view class="yy-cel-reson">
			<text>其他原因</text>
			<view class="yy-cel-more">
				<textarea class="yy-cel-moreCon" @input="assignment" @focus="cancelsz" v-model="moreReason" value="" maxlength="50" placeholder="请输入原因" />
			</view>
		</view>
		<view class="yy-cel-btn" @click="submit">提交</view>
	</view>
</template>

<script>
	import app from '../../App.vue'
	export default{
		data(){
			return{
				moreReason:'',
				reasonlist:[],
				reason:'',
				order_id:''
			}
		},
		onLoad(options) {
			this.order_id = options.order_id
		},
		onShow() {
			this.getreason()
		},
		methods:{
			getreason(){				//原因
				let that = this,
				data = {
					type:'6'
				}
				app.post('api/common/getConfig',data).then(res=>{
					if(res.code == 1){
						console.log(res)
						let reasonlist = res.data.reason
						reasonlist.forEach(res=>{
							res.check = false
						})
						that.reasonlist = reasonlist
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none',
							mask: true
						})
					}
				}).catch(err=>{
					uni.showToast({
						title: err.msg,
						icon: 'none',
						mask: true
					})
				})
			},
			choosereason(name,i){			//选择现有的原因
				let that = this,
				list = that.reasonlist
				that.moreReason = ''
				that.reason = name
				list.forEach((res,index)=>{
					res.check = false
					if(i == index){
						res.check = true
					}
				})
				that.reasonlist = list
				console.log(that.reasonlist)
			},
			cancelsz(){					//选择其他原因
				let that = this,
				list = that.reasonlist
				list.forEach(res=>{
					res.check = false
				})
				that.reasonlist = list
			},
			assignment(){
				this.reason = this.moreReason
			},
			submit(){
				let that = this,
				data = {
					order_id:that.order_id,
					reason:that.reason
				}
				console.log(that.reason)
				app.post('api/order/cancelOrder',data).then(res=>{
					if(res.code == 1){
						uni.showToast({
							title: res.msg,
							icon: 'success',
							mask: true
						})
						setTimeout(function(){
							uni.showLoading({
								title:'加载中',
								success() {
									setTimeout(function(){
										uni.reLaunch({
											url:'/pages/person/detail?order_id='+that.order_id
										})
									},1300)
								}
							})
						},500)
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none',
							mask: true
						})
					}
				}).catch(err=>{
					uni.showToast({
						title: err.msg,
						icon: 'none',
						mask: true
					})
				})
			}
		}
	}
</script>

<style>
	@import url("../../common/yuyuecancel.css");
</style>
